package com.ywb.ex10;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.Vector;

public class Test02 extends JFrame {
    Test02(){
        super();
        setTitle("定义表格");
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] thead = { "学号", "姓名", "性别", "年龄", "身高", " 体重", "血型" };

        Vector columnNames = new Vector(Arrays.asList(thead));

        Vector columnValues = new Vector<>();
        for (int row = 1; row < 21; row++) {
            Vector rowV = new Vector();
            for (int column = 0; column < thead.length; column++)
            {
                rowV.add(thead[column] + row);
            }
            columnValues.add(rowV);
        }

        JTable table = new JTable(columnValues,columnNames);
//        设置table属性
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setSelectionBackground(Color.YELLOW);
        table.setSelectionForeground(Color.RED);
        table.setRowHeight(30);
//        设置cell单元格属性居中
        DefaultTableCellRenderer render = new DefaultTableCellRenderer();
       render.setHorizontalAlignment(SwingConstants.CENTER);
       table.setDefaultRenderer(Object.class, render);

        final JScrollPane scrollPane = new JScrollPane(table);

        getContentPane().add(scrollPane);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Test02().setVisible(true));
    }
}
